package com.whh.cos.action;

import com.whh.cos.model.ObjectInfo;
import com.whh.cos.service.BucketService;
import com.whh.cos.service.ObjectService;
import com.whh.cos.system.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author huahui.wu.
 *         Created on 2018/1/17.
 */
@Controller

public class IndexAction {
    @Resource
    private ObjectService objectService;
    @Resource
    private BucketService bucketService;

    @GetMapping(value = "/index")
    public ModelAndView index() throws ServiceException {
        String bucketName = "cos-1251506627";
        if (!bucketService.headBucket(bucketName)) {
            throw new ServiceException(100102, "bucket name is not exist");
        }
        List<ObjectInfo> objectInfos = objectService.listObjects(bucketName, null);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/index");
        mav.addObject("objects", objectInfos);
        mav.addObject("bucketName", bucketName);
        return mav;
    }
}
